﻿using WRJClient.DAL;
using Esri.ArcGISRuntime.Geometry;
using WRJClient.Common;
using WRJClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WRJClient.UI.Map;

namespace WRJClient.UI.OverallState
{
    /// <summary>
    /// DataMonitor.xaml 的交互逻辑
    /// </summary>
    public partial class DataMonitor : UserControl
    {

        private BaseMap _mapControl;
        public DataMonitor()
        {
            InitializeComponent();
            init();
        }

        public void SetMapControl(BaseMap mapControl)
        {
            _mapControl = mapControl;
        }

        private void init()
        {
            controlcenterlist.ItemsSource = ControlCenterManager.Instance.Datas;
            workstationlist.ItemsSource = WorkStationManager.Instance.Datas;
            uavlist.ItemsSource = UAVManager.Instance.Datas;
            zonelist.ItemsSource = ZoneManager.Instance.Datas;
            airlinelist.ItemsSource = AirlineManager.Instance.Datas;
            infomationlist.ItemsSource = InfomationManager.Instance.Datas;
        }

        private void panToPoint(MapPoint point)
        {
            _mapControl?.PanTo(point);
        }

        private void setViewPoint(Esri.ArcGISRuntime.Geometry.Geometry geometry)
        {
            _mapControl?.SetViewPoint(geometry);
        }
        private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox != null)
            {
                ControlCenterViewModel controlCenter = listBox.SelectedItem as ControlCenterViewModel;
                if (controlCenter != null && controlCenter.IsVisable)
                {
                    MapPoint point = new MapPoint(controlCenter.StaticData.Longitude, controlCenter.StaticData.Latitude, SpatialReferences.Wgs84);
                    panToPoint(point);
                }
                WorkStationViewModel workstation = listBox.SelectedItem as WorkStationViewModel;
                if (workstation != null && workstation.IsVisable)
                {
                    MapPoint point = new MapPoint(workstation.StaticData.Longitude, workstation.StaticData.Latitude, SpatialReferences.Wgs84);
                    panToPoint(point);
                }
                UAVViewModel uav = listBox.SelectedItem as UAVViewModel;
                if (uav != null && uav.IsVisable && uav.Telemetry != null)
                {
                    MapPoint point = new MapPoint(uav.Telemetry.Longitude, uav.Telemetry.Latitude, SpatialReferences.Wgs84);
                    panToPoint(point);
                }
                ZoneViewModel zone = listBox.SelectedItem as ZoneViewModel;
                if (zone != null && zone.IsVisable)
                {
                    BaseGraphic graphic = zone.GArea;
                    if (graphic != null)
                    {
                        setViewPoint(graphic.Geometry);
                    }
                }
                InfomationViewModel infomation = listBox.SelectedItem as InfomationViewModel;
                if (infomation != null && infomation.IsVisable)
                {
                    char[] spliter = { ',' };
                    string[] position = infomation.StaticData.Position.Split(spliter);
                    if (position.Count() >= 2)
                    {
                        if (double.TryParse(position.ElementAt(0), out double x) && double.TryParse(position.ElementAt(1), out double y))
                        {
                            MapPoint point = new MapPoint(x, y, SpatialReferences.Wgs84);
                            panToPoint(point);
                        }
                    }
                }
                AirlineViewModel airline = listBox.SelectedItem as AirlineViewModel;
                if (airline != null && airline.IsVisable)
                {
                    BaseGraphic graphic = airline.GBody;
                    if (graphic != null)
                    {
                        setViewPoint(graphic.Geometry);
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            if (button != null)
            {
                UAVViewModel uavViewModel = button.Tag as UAVViewModel;
                if (uavViewModel != null)
                {
                    uavViewModel.ClearFlightPoint();
                }
            }
        }
    }
}
